package net.caixiaomi.info.ui.football;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 胜平负 / 让球胜平负
 */

public class VictoryAndDefeatProxy extends ItemViewProxy<MultiItemEntity, BaseViewHolder> {
    private FootballAdapter mAdapter;

    public VictoryAndDefeatProxy(FootballAdapter adapter) {
        super(adapter);
        mAdapter = adapter;
    }

    @Override
    public void bindView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            PlayFootballItem model = (PlayFootballItem) item;
            MatchPlaysList matchPlaysList = model.getMatchPlays().get(0);
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String time = sdf.format(new Date(Long.parseLong(model.getBetEndTime()) * 1000));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(model.getLeagueAddr()).append("\n").append(model.getChangci()).append("\n").append(CommonApp.getInstance().getString(R.string.C_END_TIME)).append(time);
            helper.setText(R.id.text, stringBuilder);
            helper.addOnClickListener(R.id.content);
            stringBuilder.setLength(0);

            ElementCellModel homeCell = matchPlaysList.getHomeCell();
            homeCell.setPlayType(model.getPlayType());
            stringBuilder.append(homeCell.getCellName()).append(homeCell.getCellOdds());
            String term = stringBuilder.toString();
            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getHomeTeamRank())) {
                stringBuilder.append("[").append(model.getHomeTeamRank()).append("]");
            }
            stringBuilder.append(model.getHomeTeamAbbr());
            stringBuilder.append("\n").append(term);
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), homeCell.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.getView(R.id.host).setSelected(homeCell.isSelected());
            helper.setText(R.id.host, spannableString);

            helper.setTag(R.id.host, R.id.item_model, model);
            helper.setTag(R.id.host, R.id.item_cell, homeCell);

            helper.setTextColor(R.id.host, ContextCompat.getColor(CommonApp.getInstance(), homeCell.isSelected() ? R.color.white : R.color.primary_text));
            helper.setOnClickListener(R.id.host, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            stringBuilder.setLength(0);
            ElementCellModel flatCell = matchPlaysList.getFlatCell();
            flatCell.setPlayType(model.getPlayType());
            stringBuilder.append(flatCell.getCellName()).append(flatCell.getCellOdds());
            term = stringBuilder.toString();
            stringBuilder.setLength(0);

            String fixedOdds = matchPlaysList.getFixedOdds();//+1 、-1
            if (TextUtils.isEmpty(fixedOdds)) {
                stringBuilder.append("VS").append("\n").append(term);
            } else {
                stringBuilder.append("VS ").append(fixedOdds).append("\n").append(term);
            }

            spannableString = new SpannableString(stringBuilder);
            if (!TextUtils.isEmpty(fixedOdds)) {
                int start = stringBuilder.indexOf(fixedOdds);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), flatCell.isSelected() ? R.color.white : fixedOdds.startsWith("+") ? R.color.orange_primary : R.color.green)), start, start + fixedOdds.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, start + fixedOdds.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), flatCell.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.tie, spannableString);
            helper.getView(R.id.tie).setSelected(flatCell.isSelected());

            helper.setTag(R.id.tie, R.id.item_model, model);
            helper.setTag(R.id.tie, R.id.item_cell, flatCell);

            helper.setTextColor(R.id.tie, ContextCompat.getColor(CommonApp.getInstance(), flatCell.isSelected() ? R.color.white : R.color.third_text));
            helper.setOnClickListener(R.id.tie, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            ElementCellModel visitingCell = matchPlaysList.getVisitingCell();
            visitingCell.setPlayType(model.getPlayType());
            stringBuilder.setLength(0);
            stringBuilder.append(visitingCell.getCellName()).append(visitingCell.getCellOdds());
            term = stringBuilder.toString();
            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getVisitingTeamRank())) {
                stringBuilder.append("[").append(model.getVisitingTeamRank()).append("]");
            }
            stringBuilder.append(model.getVisitingTeamAbbr());
            stringBuilder.append("\n").append(term);
            spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), visitingCell.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - term.length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.guest, spannableString);
            helper.setTextColor(R.id.guest, ContextCompat.getColor(CommonApp.getInstance(), visitingCell.isSelected() ? R.color.white : R.color.primary_text));
            helper.getView(R.id.guest).setSelected(visitingCell.isSelected());

            helper.setTag(R.id.guest, R.id.item_model, model);
            helper.setTag(R.id.guest, R.id.item_cell, visitingCell);

            helper.setOnClickListener(R.id.guest, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                    PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                    if (mAdapter.getListener() != null) {
                        if (mAdapter.getListener().canChange(tagModel)) {
                            tag.setSelected(!tag.isSelected());
                            if (tagModel.getSelectCell() == null) {
                                tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                            }
                            if (tag.isSelected()) {
                                tagModel.getSelectCell().add(tag);
                            } else {
                                tagModel.getSelectCell().remove(tag);
                            }
                            mAdapter.getListener().onItemChangeListener(tagModel);
                        }
                    }
                }
            });

            helper.setVisible(R.id.action, TextUtils.equals(matchPlaysList.getSingle(), "1"));
            helper.setGone(R.id.btn_float, TextUtils.equals("1", model.isShutDown()));
            helper.addOnClickListener(R.id.btn_float);

            if (helper.getAdapterPosition() + 1 <= mAdapter.getData().size() - 1) {
                MultiItemEntity nextItem = mAdapter.getData().get(helper.getAdapterPosition() + 1);
                helper.setGone(R.id.line, nextItem.getItemType() == item.getItemType());
            } else {
                helper.setGone(R.id.line, false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addItemType() {
        mAdapter.addItemType(Constants.ITEM_VIEW_HEADER, R.layout.header_football);
        mAdapter.addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_victory_and_defeat);
    }
}
